package com.ccepc.algmanage.controller;

import cn.hutool.json.JSONObject;
import com.ccepc.admin.api.feign.RemoteMessageService;
import com.ccepc.algmanage.service.IdentificationRecordService;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.algmanage.utils.HkUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.common.security.annotation.Inner;
import com.ccepc.demo.api.Hk.HkAuths;
import com.ccepc.demo.api.dto.HkAuthsDto;
import com.ccepc.demo.api.entity.IdentificationRecord;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.obs.services.ObsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/hkEvent")
@Slf4j
@Api(value = "hkEvent", tags = "海康事件")
public class HkEventController {

    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.access-key}")
    private String accessKey;
    @Value("${oss.secret-key}")
    private String secretKey;
    @Value("${oss.bucket-name}")
    private String bucketName;

    @Resource
    private IdentificationRecordService identificationRecordService;

    @ApiOperation(value = "接收订阅消息")
    @SysLog("接收订阅消息" )
    @PostMapping("/callback")
    @Inner(value = false)
    public ResponseEntity<Object> algCallBack(@RequestBody String request){
        log.info("回调成功，回调信息："+request);
        if (request == null) {
            log.info("请求参数为空");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
        return identificationRecordService
                .saveHkRecords(request, new ObsClient(accessKey, secretKey, endpoint), bucketName);
    }

    @ApiOperation(value = "通过id删除联动记录")
    @SysLog("通过id删除联动记录" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('algmanage_identificationrecord_del')" )
    public ResponseEntity<Object> removeById(@PathVariable Integer id) {
        return identificationRecordService
                .removeRecord(id, new ObsClient(accessKey, secretKey, endpoint), bucketName);
    }

    @ApiOperation(value = "通过ids删除联动记录")
    @SysLog("通过ids删除联动记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('algmanage_identificationrecord_del')" )
    public ResponseEntity<Object> removeBatch(@RequestBody List<IdentificationRecord> records) {
        return identificationRecordService
                .removeBatch(records, new ObsClient(accessKey, secretKey, endpoint), bucketName);
    }

}
